<?php
/**
 * Save changes to extension settings as a result of the extension form.
 *
 * @copyright &copy; 2006 The Open University
 * @author s.marshall@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package resourcepage
 *//** */

require_once("module_resourcepage.php");
$module=module_resourcepage::$inst;

$coursemodule=required_param('coursemodule',PARAM_INT);    // Course Module ID
$extension=required_param('extension',PARAM_ALPHA);
$scrollpos=optional_param('scrollpos',0,PARAM_INT);

$module->obtain_view_info($coursemodule,null,$cm,$course,$resourcepage);

// Check basic course edit action requirements
$module->verify_edit_action($cm);

// Copy post variables into $form
foreach($_POST as $name=>$value) {
    $form->{$name}=$value;
}
$form->courseid=$course->id;

// Get extension object
$extobj =& $module->get_specified_extension($extension);

$pullextension=is_a($extobj,'resourcepage_extension_pull');

$tw=new transaction_wrapper();

if($pullextension) {
    $containerid=required_param('containerid',PARAM_INT);
    $containertype='pull_'.$extobj->get_name();

    if($containerid==0) {

        try {
            $module->add_pull_extension($resourcepage->id, $extobj,$resourcepage->containerid,$form);
        } catch(Exception $e) {
            error($e->getMessage());
        }

        $logtype='addext';
        $logdetail="Added new extension #$containerid";
    } else { // Modify existing instance
        global $CFG;

        // Check container is in this course & belongs to this extension
        $module->verify_container_editable(
            array($resourcepage->containerid,$resourcepage->deletedcontainerid),
            $containerid,true,$containertype);

        /*
         * If the processing is for the rssfeed extension, then this code determines
         * the current display sequence of the rssfeed container, creates a new rssfeed
         * as requested or references and existing rssfeed container.
         * Either way, a new rssfeed container is created, and moved to the correct
         * display sequence, and the previous rssfeed container is removed (not deleted)
         * from the parent container.
         *
         */
        if ($form->extension == 'rssfeed') {
            $sql = <<<EOF
SELECT *
FROM   {$CFG->prefix}resourcepage_containeritems
WHERE  containerid = {$resourcepage->containerid}
AND    subcontainerid = {$containerid}
LIMIT  1
EOF;

            $containeritems = get_records_sql($sql);
            $itemsequence = FALSE;
            if ($containeritems !== FALSE) {
                foreach ($containeritems as $containeritem) {
                    $itemsequence = $containeritem->sequence;
                }
            }

            try {
                $module->delete_container_subcontainer($resourcepage->containerid,$containerid);
                $newcontainerid = $module->add_pull_extension($resourcepage->id, $extobj,$resourcepage->containerid,$form);
                $module->move_container_subcontainer($resourcepage->containerid,$newcontainerid,$resourcepage->containerid,$itemsequence);
            } catch(Exception $e) {
                error($e->getMessage());
            }
        } else {

            /*
             * If not rssfeed extension, then do normal operation.
             *
             */

            // Update existing instance in table
            $extobj->update_instance($form);

        }

        // Run extension update now
        $plugininfo=$module->do_sql(
            'SELECT plugininfo FROM modprefix_containers WHERE id='.$containerid)->fields['plugininfo'];
        try {
            $module->update_extension($containerid,$extobj,$plugininfo);
        } catch(Exception $e) {
            error($e->getMessage());
        }

        $logtype='updateext';
        $logdetail="Updated extension #$containerid";
    }
} else {
    $placeholderitemid=required_param('placeholderitemid',PARAM_INT);

    $logtype='updateph';
    $logdetail="Updated placeholder $placeholderitemid for course #{$course->id}";
    $extobj->addupdate_instance($placeholderitemid,$form);
}
$tw->commit();

// Log and return to view
add_to_log($course->id, 'resourcepage', $logtype, 'view.php?id='.$cm->id,
    $logdetail,$cm->id);
redirect('view.php?id='.$cm->id.($scrollpos ? '&scrollpos='.$scrollpos : ''));

?>
